﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon.ChildWindows;

namespace Baosight.ZNH.ZMCE.UI.PopWindows
{
    public partial class EquipmentCard : EFViewCmes
    {
        public EquipmentCard(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    base.Mode = OperationMode.Insert;
                    this.fdIsFlinging.Value = "否";
                    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    break;
                case "detail":
                    this.Commit.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.spWindows.GetChildObjects<EFDataField>(null, true));
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 提交按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            base.OnCommitButtonClick(sender, e);
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        /// <summary>
        /// 设备分类改变后修改一些字段的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdEquipmentClassId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (e.NewValue == null || "".Equals(e.NewValue))
            {
                return;
            }
            EFDataField field = sender as EFDataField;
            CmesCustomView picker = field.Content as CmesCustomView;
            if (picker.ItemObject != null)
            {
                this.fdToencode.Value = picker.ItemObject.GetType().GetProperty("fdToencode").GetValue(picker.ItemObject, null).ToString();                
            }
        }

        /// <summary>
        /// 使用状态改变，部门和工程是否必填
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdUsingType_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }
            if (!("在库".Equals(e.NewValue.ToString()) || "报废".Equals(e.NewValue.ToString())))
            {
                this.fdDepartmentId.Label = "*使用部门";
                this.fdDepartmentId.eRegex = "^.+$";
                this.fdDepartmentId.eValidateErrorPrompt = "不能为空";
                return;
            }
            this.fdDepartmentId.Label = "使用部门";
            this.fdDepartmentId.eRegex = "";
            this.fdDepartmentId.eValidateErrorPrompt = "";
        }

        /// <summary>
        /// 部门
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdDepartmentId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFComboBox box = (sender as EFDataField).Content as EFComboBox;
            if (box.SelectedItem != null)
            {
                this.fdDepartment.Value = box.SelectedItem.GetType().GetProperty("orgName").GetValue(box.SelectedItem, null).ToString();                
            }
        }

        /// <summary>
        /// 工程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdEngineeringId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFComboBox box = (sender as EFDataField).Content as EFComboBox;
            if (box.SelectedItem != null)
            {
                this.fdEngineering.Value = box.SelectedItem.GetType().GetProperty("fdName").GetValue(box.SelectedItem, null).ToString();
            }
        }

        /// <summary>
        /// 临时设备可以不输入设备编码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdIsFlinging_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if ("是".Equals(this.fdIsFlinging.Value))
            {
                this.fdCode.eLabel = "设备编号";
                this.fdCode.eRegex = "^[a-zA-Z0-9]{0,20}$";
                this.fdCode.eValidateErrorPrompt = "输入0~20个数字、字母";
                this.fdCode.Value = "";
            }
            else
            {
                this.fdCode.eLabel = "*设备编号";
                this.fdCode.eRegex = "^[a-zA-Z0-9]{1,20}$";
                this.fdCode.eValidateErrorPrompt = "输入1~20个数字、字母";
            }
        }

    }
}
